package com.tusuo.stds.ui.password;

import com.andreabaccega.widget.FormEditText;
import com.hannesdorfmann.mosby.mvp.MvpBasePresenter;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.biz.CurrentUser;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.ApiResult;
import com.tusuo.stds.ui.pay.MyPayPresenter;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;

/**
 * Created by developer on 17-8-29.
 */

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(PasswordPresenter.class)
public class PasswordPresenter extends MvpBasePresenter<PasswordView> {

    @Inject
    ApiHelper mApiHelper;

    @Inject
    CurrentUser mUser;

    PasswordPresenter() {
        Injector.initPasswordPresenterComponent().inject(this);
    }

    @Override
    public void attachView(PasswordView view) {
        super.attachView(view);
    }

    @Override
    public void detachView(boolean retainInstance) {
        super.detachView(retainInstance);
    }

    public void commitPassword(String newPassword,String oldPassword) {
        mApiHelper.modifyPassword(new ModifyPasswordSubscriber(), oldPassword, newPassword);
    }

    private class ModifyPasswordSubscriber extends Subscriber<ApiResult> {

        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
        }

        @Override
        public void onNext(ApiResult apiResult) {
            if (getView() != null) {
                if (apiResult.ErrorCode == 0) {
                    mUser.logout();
                    getView().setOnActivityResult();
                } else {
                    getView().showErrMsg(apiResult.ErrorMessage);
                }
            }
        }
    }

}
